//
//  workerManagerVC.m
//  Recycling
//
//  Created by jzd on 2018/6/4.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "workerManagerVC.h"
#import "selectAreaTabVC.h"
#import "areaModel.h"
#import "selectDateVC.h"
#import "transitionAnnimater.h"

#import "nomalTableViewCell.h"
#import "nomalCollectionViewCell.h"


@interface workerManagerVC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIButton *startBut;
@property (weak, nonatomic) IBOutlet UIButton *stopBut;
@property (weak, nonatomic) IBOutlet UIButton *areaBut;
@property (weak, nonatomic) IBOutlet UILabel *allOrderLab;
@property (weak, nonatomic) IBOutlet UILabel *completeOrderLab;
@property (weak, nonatomic) IBOutlet UILabel *waitOrderLab;
@property (weak, nonatomic) IBOutlet UILabel *cancelOrderLab;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong,nonatomic)NSArray *nameArray;//名字数组
@property (strong,nonatomic)NSMutableArray *dataArray;

@property(strong,nonatomic)transitionAnnimater *transitionVC;
@property (strong,nonatomic)areaModel *areaM;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;




@property(strong,nonatomic)NSArray *collectionViewTit;//传过去的header内容
@property(strong,nonatomic)NSArray *flagArray;//按照这个键取数组
@end

@implementation workerManagerVC

-(NSArray *)collectionViewTit{
    if (!_collectionViewTit) {
        _collectionViewTit = @[@{@"title":@"接单量",@"price":@"单"},@{@"title":@"已完成单量",@"price":@"单"},@{@"title":@"未完成单量",@"price":@"单"},@{@"title":@"已取消单量",@"price":@"单"},@{@"title":@"已作废单量",@"price":@"单"},@{@"title":@"信息费总额",@"price":@"元"},@{@"title":@"账户余额",@"price":@"元"},@{@"title":@"完工率",@"price":@"%"}];
    }
    return _collectionViewTit;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSArray *)flagArray{
    if (!_flagArray) {
        _flagArray = @[@"TotalCount",@"CompleteCount",@"UnCompleteCount",@"CancelCount",@"InvalidCount",@"TotalMsgAmount",@"Balance",@"CompleteRate"];
    }
    return _flagArray;
}
-(transitionAnnimater *)transitionVC{
    if (_transitionVC == nil) {
        _transitionVC = [[transitionAnnimater alloc]init];
        _transitionVC.torect = CGRectMake(0, SJScreenH-260, SJScreenW, 260);
    }
    return _transitionVC;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"师傅管理";
    
    self.topCons.constant = statsBarHeight+44;

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *date = [formatter stringFromDate:[NSDate date]];
    [self.startBut setTitle:date forState:UIControlStateNormal];
    [self.stopBut setTitle:date forState:UIControlStateNormal];
    
    self.tableview.bounces = NO;
    self.collectionView.bounces = NO;
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([nomalCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"collectionViewCell"];
    [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([nomalTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"nomalCell"];
    
    [self getData];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(CollectionViewCellScroll:) name:scrollDidScNotification object:nil];
}
-(void)CollectionViewCellScroll:(NSNotification *)noti{
    UIScrollView *sc = noti.object;
    self.tableview.contentOffset  = sc.contentOffset;
    for (nomalCollectionViewCell *cell in self.collectionView.visibleCells) {
        cell.tableview.contentOffset = sc.contentOffset;
    }
}
-(void)getData{
    
    [self.dataArray removeAllObjects];
    NSDictionary *dict = @{@"uid":@([UserInfoTool shareUserInfoTool].Info.ID),@"wid":@""};
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    if (self.startBut.titleLabel.text.length != 0) {
        [params setObject:self.startBut.titleLabel.text forKey:@"sdate"];
    }else{
        [params setObject:@"" forKey:@"sdate"];
    }
    if (self.stopBut.titleLabel.text.length != 0) {
        [params setObject:self.stopBut.titleLabel.text forKey:@"edate"];
    }else{
        [params setObject:@"" forKey:@"edate"];
    }
    if (![self.areaBut.titleLabel.text isEqualToString:@"请选择"]) {
        [params setObject:self.areaM.code forKey:@"areacode"];
    }else{
        [params setObject:@"" forKey:@"areacode"];
    }
    [[SJNetWorkTool sharedTool]requestWithURLString:GetWorkerOrderSta parameters:params method:POST callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    NSArray *arr = [data objectForKey:@"rows"];
                    if (arr.count != 0) {
                        self.nameArray = [arr valueForKeyPath:@"WorkerName"];
                        self.allOrderLab.text = [[data objectForKey:@"total"] stringValue];
                        self.completeOrderLab.text = [[data objectForKey:@"SumCompleteCount"] stringValue];
                        self.waitOrderLab.text = [[data objectForKey:@"SumUnCompleteCount"] stringValue];
                        self.cancelOrderLab.text = [[data objectForKey:@"SumCancelCount"] stringValue];
                        //分键取值
                        for (int i = 0; i<self.flagArray.count; i++) {
                            NSArray *keyArray = [arr valueForKey:[self.flagArray objectAtIndex:i]];
                            [self.dataArray addObject:keyArray];
                        }

                    }else{
                        [self.navigationController.view showToast:@"没有更多数据" completion:nil];
                        return ;
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableview reloadData];
        [self.collectionView reloadData];

        

    }];
}
//查询按钮被点击
- (IBAction)checkData:(UIButton *)sender {

//    NSArray *arr = self.collectionView.visibleCells;
//    for (nomalCollectionViewCell *cell in arr) {
//        [cell.tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
//    }
//    [self.tableview setScrollsToTop:YES];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"goTop" object:nil];
    
    [self getData];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableview) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"goScroll" object:scrollView];
    }
}
- (IBAction)selectArea:(UIButton *)sender {
    //区域按钮
    
    selectAreaTabVC *vc = [[selectAreaTabVC alloc]init];
    
    __block __weak workerManagerVC *Copy_self = self;
    vc.resultBlock = ^(areaModel *model) {
        Copy_self.areaM = model;
        [Copy_self.areaBut setTitle:model.name forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)startData:(UIButton *)sender {
    selectDateVC *vc = [[selectDateVC alloc]init];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate  = self.transitionVC;
    //    __block __weak controlCenterWorkVC *copy_self = self;
    vc.dateBlock = ^(NSString *date) {
        [sender setTitle:date forState:UIControlStateNormal];
    };
    [self presentViewController:vc animated:YES completion:nil];
}
- (IBAction)stopData:(UIButton *)sender {
    selectDateVC *vc = [[selectDateVC alloc]init];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate  = self.transitionVC;
    //    __block __weak controlCenterWorkVC *copy_self = self;
    vc.dateBlock = ^(NSString *date) {
        [sender setTitle:date forState:UIControlStateNormal];
    };
    [self presentViewController:vc animated:YES completion:nil];
}




#pragma mark --tableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    nomalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nomalCell"];
    cell.titleL.text = [self.nameArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableview.width, 70)];
    back.backgroundColor = [UIColor whiteColor];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, back.width, back.height-1)];
    lab.text = @"师傅姓名";
    lab.textColor = [UIColor darkGrayColor];
    lab.font = [UIFont systemFontOfSize:15];
    lab.textAlignment = NSTextAlignmentCenter;
    [back addSubview:lab];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, back.height-1, back.width, 1)];
    line.backgroundColor = RGBA(67, 67, 67, 0.5);
    [back addSubview:line];
    return back;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 70;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0){
    nomalCollectionViewCell *cc = (nomalCollectionViewCell *)cell;
    cc.tableview.contentOffset = self.tableview.contentOffset;
}

#pragma mark --collectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return 0;
    }else{
        return self.dataArray.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    nomalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    
    cell.headerTit = [self.collectionViewTit objectAtIndex:indexPath.row];
    cell.numArray = [self.dataArray objectAtIndex:indexPath.item];
//    cell.contentOff = self.tableview.contentOffset;
    return cell;
}
#pragma mark --collectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SJScreenW-85)*0.5, self.collectionView.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

@end
